﻿<#
.SYNOPSIS  
    Creates the web applications associated to the content databases to be upgraded.  Adds web application policies and alternate access mappings.  
.DESCRIPTION  
    This script creates the web applications specified in the Upgrade.xml file.  The SharePoint 2007 content databases will be attached to these web applications and upgraded. 
.NOTES
    Any web application that already exists will be skipped.
.LINK  
    http://7up.codeplex.com 
#>
Write-Host ("   Adding web applications ({0})..." -f [DateTime]::Now.ToString())

#add SharePoint snap in
if ((Get-PSSnapin -Name  Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
{ 
    Add-PsSnapin  Microsoft.SharePoint.PowerShell 
}

#get config
$config = [xml](Get-Content Upgrade.xml)

foreach($webApp in $config.Settings.WebApplications.WebApplication)
{
	$theApp = (Get-SPWebApplication $webApp.Name -ErrorAction SilentlyContinue)
	if($webApp -ne $null)
	{
		if($theApp -eq $null)
		{
			Write-Host ("      Adding web application '{0}' ({1})..." -f $webApp.Name, [DateTime]::Now.ToString())
			$theApp = (New-SPWebApplication -Name $webApp.Name -Port $webApp.Port -HostHeader $webApp.HostHeader -Url $webApp.Url -ApplicationPool $webApp.ApplicationPool -Path $webApp.Path -ApplicationPoolAccount (Get-SPManagedAccount $webApp.ApplicationPoolIdentity))
			$theApp.ContentDatabases | Remove-SPContentDatabase -Confirm:$false -Force:$true
		}
		else
		{
			Write-Host ("      Skipping web application '{0}' because it already exists ({1})." -f $webApp.Name, [DateTime]::Now.ToString())
		}
	
		if($webApp.ServiceApplicationProxyGroup -ne "[default]")
		{
			Write-Host ("         Setting web application '{0}' proxy group to '{1}' ({2})..." -f $webApp.Name, $webApp.ServiceApplicationProxyGroup, [DateTime]::Now.ToString())
			$proxyGroup = (Get-SPServiceApplicationProxyGroup | Where-Object {$_.FriendlyName -eq $webApp.ServiceApplicationProxyGroup})
			if($proxyGroup -eq $null)
			{
				Write-Host ("         Creating web application '{0}' proxy group ({1})..." -f $webApp.ServiceApplicationProxyGroup, [DateTime]::Now.ToString())
				$proxyGroup = (New-SPServiceApplicationProxyGroup -Name $webApp.ServiceApplicationProxyGroup)
			}
			$theApp.ServiceApplicationProxyGroup = $proxyGroup
			$theApp.Update()
		}
	}

    #add user policies
	Write-Host ("   Adding policies ({0})..." -f [DateTime]::Now.ToString())
	$theApp = (Get-SPWebApplication $webApp.Name)
	foreach($policy in $webApp.Policies.Policy)
	{
		if($policy -ne $null)
		{
			$thePolicy = $theApp.Policies | Where-Object {$_.DisplayName -eq $policy.DisplayName}
			if($thePolicy -eq $null)
			{
				Write-Host ("         Adding web application policy '{0}' ({1})..." -f $policy.DisplayName, [DateTime]::Now.ToString())
				$thePolicy = $theApp.Policies.Add($policy.UserName,$policy.DisplayName)
				
				foreach($role in $policy.Roles.Role)
				{
					if($role -ne $null)
					{
						$binding = $theApp.PolicyRoles | Where-Object {$_.Name -eq $role.Name}
						if($binding -ne $null)
						{
							Write-Host ("            Adding policy role '{0}' ({1})..." -f $role.Name, [DateTime]::Now.ToString())
							$thePolicy.PolicyRoleBindings.Add($binding)
							$theApp.Update()
						}
						else
						{
							Write-Error ("           Web application policy role '{0}' does not exist." -f $role.Name)
						}
					}
				}
			}
			else
			{
				Write-Host ("         Skipping web application policy '{0}' because it already exists ({1})." -f $policy.DisplayName, [DateTime]::Now.ToString())
			}
		}
	}

	#add aams
	Write-Host ("   Adding alternate access mappings ({0})..." -f [DateTime]::Now.ToString())
	foreach($aam in $webApp.AlternateAccessMappings.AlternateAccessMapping)
	{
		if($aam -ne $null)
		{
			$theAAM = ($theApp.AlternateUrls | Where-Object {$_.IncomingUrl -eq $aam.Url -and $_.Zone -eq $aam.Zone})

			if($theAAM -eq $null)
			{
				Write-Host ("         Adding alternate access mapping '{0}-{1}' ({2})..." -f $aam.Url, $aam.Zone, [DateTime]::Now.ToString())
				$alternateUrl = New-Object Microsoft.SharePoint.Administration.SPAlternateUrl($aam.Url, $aam.Zone)
				$theApp.AlternateUrls.Add($alternateUrl)
			}
			else
			{
				Write-Host ("         Skipping alternate access mapping '{0}-{1}' because it already exists ({2})." -f $aam.Url, $aam.Zone, [DateTime]::Now.ToString())
			}
		}
	}
}

iisreset